package drinkkiosk;

import serialtool.SerialTool;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import networktool.KioskServer;

/**DrinkKiosk
 * This program provides a private club or resort with the ability for Patrons
 * to order Drinks from conveniently located kiosks. An easy to navigate UI with
 * large buttons is ideal for touch screen displays. Patrons may select from 
 * Specials determined by ambient temperature from an embedded board, or they may
 * construct a CustomDrink from lists of ingredients. Once a Drink is ready to
 * be ordered, the Patrons enters their table number and a confirmation is displayed
 * informing the Patron that their Drink is on the way. The Drink is sent over
 * the LAN to the BartenderQueue server where the Drink will be displayed for a
 * Bartender to prepare, and for Wait Staff to serve.
 *
 * @author Charles Ritchea
 */
public class DrinkKiosk extends javax.swing.JFrame {
    private static  int port;
    private static Socket server;
    private static ServerSocket listener;

    /** Creates new form DrinkKiosk */
    public DrinkKiosk(){
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents(){

        cardPanel1 = new drinkkiosk.CardPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        setUndecorated(true);

        cardPanel1.setMaximumSize(new java.awt.Dimension(800, 600));

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Welcome to the Resort Drink Kiosk");
        jLabel1.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE).addComponent(cardPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE).addGap(0, 0, 0).addComponent(cardPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 559, javax.swing.GroupLayout.PREFERRED_SIZE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Settings.loadSettings();
        port = Integer.parseInt(Settings.getKioskServerPort());
	    final DrinkKiosk drinkKiosk = new DrinkKiosk();
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DrinkKiosk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DrinkKiosk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DrinkKiosk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DrinkKiosk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            private String buttonStatus;

            /**
             * Test if Full-Screen Exclusive Mode is supported, and
             * if it is, then switch to Full-Screen at 800x600 resolution.
             */
            @Override
            public void run() {
			drinkKiosk.initComponents();
                    drinkKiosk.setAlwaysOnTop(true);
                    drinkKiosk.setVisible(true);


                try {
                    SerialTool serialTool = new SerialTool();
                    serialTool.getResult("led1");
                } catch (java.lang.Throwable t) {
                }


               goFullScreen();



            }
        });
		try {
			listener = new ServerSocket(port);
			while(true){
				server = listener.accept();
				KioskServer kioskServer = new KioskServer(server, drinkKiosk.cardPanel1);
				Thread t = new Thread(kioskServer);
				t.start();
			}
		} catch (IOException ex) {
			Logger.getLogger(KioskServer.class.getName()).log(Level.SEVERE, null, ex);
		}
    }

    private static void goFullScreen() {
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
        if (graphicsDevices[0].isFullScreenSupported()) {
            graphicsDevices[0].setFullScreenWindow(getWindows()[0]);
            DisplayMode[] dms = graphicsDevices[0].getDisplayModes();
            graphicsDevices[0].setDisplayMode(new DisplayMode(800, 600, 32, 60));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private drinkkiosk.CardPanel cardPanel1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
